/*
 * Explosive.java
 *
 * Created on 2007. mC!rcius 12., 17:37
 *
 */

package laborhazi;

/**
 * Blows up lemmings and surroundings
 * @author nyuszik
 */
public class Explosive extends Tool{
    private int power = 4;
    // private Test test; //just for testing
    private Field field; //just for testing
    /** Creates a new instance of Explosive
     * @param f the Field
     */
    public Explosive() {
    	
    }
    public Explosive(Field f) {
        field = f;        
    }
    public String toString() {
    	return ("Explosive");
    }
    /** 
     * Using the explosive will start a timer, if the timer ends, the lemming is blown up, as well as the surrounding elements
     * @param lemming the lemming who uses the Explosive
     */
    public void use(Lemming lemming){
    	System.out.println(power);
    	lemming.setExploding(true);
    	lemming.setExplodingTime(power);
    	if (checkPower()) {
        	power--;
        } else {
            detonate(lemming);
        }
    }
    /** 
     * Detonates the explosive
     * @param lemming the lemming who is blown up
     */
    private void detonate(Lemming lemming){
    	int[] position = new int[2];
        int[] pos = lemming.getPosition();
        position[0] = pos[0];
        position[1] = pos[1];
        position[0] = position[0] + 1;
        if (field.check(position) != null 
        		&& !(field.check(position) instanceof Stone 
        				&& ((Stone)field.check(position)).getNomap()) ) {
        	field.blow(position);
        }
        position[0] = position[0] - 2;
        if (field.check(position) != null 
        		&& !(field.check(position) instanceof Stone 
        				&& ((Stone)field.check(position)).getNomap()) ) {
        	field.blow(position);
        }
        position[0] = position[0] + 1;
        position[1] = position[1] + 1;
        if (field.check(position) != null 
        		&& !(field.check(position) instanceof Stone 
        				&& ((Stone)field.check(position)).getNomap()) ) {
        	field.blow(position);
        }
        position[1] = position[1] - 2;
        if (field.check(position) != null 
        		&& !(field.check(position) instanceof Stone 
        				&& ((Stone)field.check(position)).getNomap()) ) {
        	field.blow(position);
        }
        position[1]++;
        field.blow(position);
        erase(lemming);
    }
    private boolean checkPower() {
        if (power > 0) return true;
        else return false;
    }
    private void erase(Lemming lemming) {
    	lemming.removeTool(5);
    }
}
